//
//  AFNetApiClient.m
//  xyl
//
//  Created by EPWK on 15-1-8.
//  Copyright (c) 2015年 YLP. All rights reserved.
//


#import "AFNetApiClient.h"

//static NSString * const AFAppNetAPIBaseURLString = @"http://59.57.179.146:8000";
static NSString * const AFAppNetAPIBaseURLString = @"http://120.25.226.171:8001";

@implementation AFNetApiClient

+ (instancetype)sharedClient {
    static AFNetApiClient *_sharedClient = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedClient = [[AFNetApiClient alloc] initWithBaseURL:[NSURL URLWithString:AFAppNetAPIBaseURLString]];
        _sharedClient.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
        [_sharedClient.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
            switch (status) {
                case AFNetworkReachabilityStatusNotReachable:
                    [SVProgressHUD showErrorWithStatus:@"无网络,请检查网络设置!"];
                    break;
                default:
                    break;
            }
        }];
        [_sharedClient.reachabilityManager startMonitoring];
    });
    
    return _sharedClient;
}

//取消网络请求
- (void)cancleOperation
{
    [[AFNetApiClient sharedClient].operationQueue cancelAllOperations];
}


/*
 *  网络请求
 *
 *  @para path API接口地址
 *  @para parameters JSON数据
 *  @para successBlock 请求成功回调
 *  @para failureBlock 请求失败回调
 */
- (void)postWithPath:(NSString *)path
          parameters:(NSDictionary *)parameters
             success:(AFAPISuccess)successBlock
             failure:(AFAPIFailure)failureBlock
{
    NSMutableDictionary *paras = [NSMutableDictionary dictionaryWithDictionary:parameters];
    
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    [self POST:path parameters:paras success:^(AFHTTPRequestOperation *operation , id responseObject) {
        if (successBlock) {
            [SVProgressHUD dismiss];
            successBlock(operation,responseObject);
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
            });
        }
    } failure:^(AFHTTPRequestOperation *operation , NSError *error) {
        if (failureBlock) {
            [SVProgressHUD dismiss];
            [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
            failureBlock(operation,error);
            
        }
    }];
}

//注册
- (void)requestRegisterWithParams:(NSDictionary *)params success:(AFAPISuccess)success failure:(AFAPIFailure)failure
{
    [self postWithPath:@"/mpay/app/cust/register.do?" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        success(operation, responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failure(operation, error);
    }];
}


//登录
- (void)requestLoginWithParams:(NSDictionary *)params success:(AFAPISuccess)success failure:(AFAPIFailure)failure
{
    [self postWithPath:@"/mpay/app/cust/login.do?" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        success(operation, responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failure(operation, error);
    }];
}
//登出
- (void)requestLogoutWithParams:(NSDictionary *)params success:(AFAPISuccess)success failure:(AFAPIFailure)failure
{
    [self postWithPath:@"" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        success(operation, responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failure(operation, error);
    }];
}

//忘记密码
- (void)requestForgetPassWordWithParams:(NSDictionary *)params success:(AFAPISuccess)success failure:(AFAPIFailure)failure
{
    [self postWithPath:@"" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        success(operation, responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failure(operation, error);
    }];
}


//关于我们
- (void)requestAboutUsWithParams:(NSDictionary *)params success:(AFAPISuccess)success failure:(AFAPIFailure)failure
{
    [self postWithPath:@"/mpay/app/cms/getContent.do?type=aboutUs" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        success(operation, responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failure(operation, error);
    }];
}

//支付通道
- (void)requestChargeChannelListWithParams:(NSDictionary *)params success:(AFAPISuccess)success failure:(AFAPIFailure)failure
{
    [self postWithPath:@"/mpay/app/fund/queryCustChannelList.do?" parameters:@{@"custNo":UserInfo[@"NUMBER"]} success:^(AFHTTPRequestOperation *operation, id responseObject) {
        success(operation, responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failure(operation, error);
    }];
}

//支付 充值
- (void)requestChargeMoneyWithParams:(NSDictionary *)params success:(AFAPISuccess)success failure:(AFAPIFailure)failure
{
    [self postWithPath:@"/mpay/app/fund/recharge.do?" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        success(operation, responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failure(operation, error);
    }];
}
//提现
- (void)requestTakeofMoneyWithParams:(NSDictionary *)params success:(AFAPISuccess)success failure:(AFAPIFailure)failure
{
    [self postWithPath:@"/mpay/app/fund/cash.do?" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        success(operation, responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failure(operation, error);
    }];
}

//支付 
- (void)requestPayMoneyWithParams:(NSDictionary *)params success:(AFAPISuccess)success failure:(AFAPIFailure)failure
{
    [self postWithPath:@"/mpay/app/fund/payment.do?" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        success(operation, responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failure(operation, error);
    }];
}

//充值记录
- (void)requestChargeHistoryWithParams:(NSDictionary *)params success:(AFAPISuccess)success failure:(AFAPIFailure)failure
{
    [self postWithPath:@"/mpay/app/fund/queryCashList.do?" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        success(operation, responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failure(operation, error);
    }];
}

//获取 用户协议type=agreement  客服中心type=custService
- (void)requestUserCenterWithParams:(NSDictionary *)params success:(AFAPISuccess)success failure:(AFAPIFailure)failure
{
    [self postWithPath:@"/mpay/app/cms/getContent.do?" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        success(operation, responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failure(operation, error);
    }];
}

@end
